package com.kun.util;

import cn.hutool.core.util.StrUtil;
import com.kun.constant.SecurityConstant;
import com.kun.entity.SysRole;
import com.kun.entity.SysUser;
import com.kun.remote.RemoteUserService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author kun.li
 */
public class LoginUserUtils {

    private final static Map<String, SysUser> userInfoCacheMap = new ConcurrentHashMap<>();


    public static SysUser getLoginUser(HttpServletRequest request, boolean isFull) {
        String username = request.getHeader(SecurityConstant.USERNAME);
        String userId = request.getHeader(SecurityConstant.USERID);
        String roles = request.getHeader(SecurityConstant.ROLES);
        String tenantId = request.getHeader(SecurityConstant.TENANT_ID);
        String hasAdmin = request.getHeader(SecurityConstant.HAS_ADMIN);
        if (StrUtil.isAllNotEmpty(username, userId)) {
            SysUser sysUser;
            if (isFull) {
                RemoteUserService remoteUserService = SpringContextHolder.getBean(RemoteUserService.class);
                sysUser = remoteUserService.getSysUser(username, userId);
            } else {
                String cacheKey = buildCacheKey(username, userId, tenantId, roles);
                sysUser = userInfoCacheMap.get(cacheKey);
                if (sysUser == null) {
                    sysUser = new SysUser();
                    sysUser.setUserId(userId);
                    sysUser.setUsername(username);
                    sysUser.setTenantId(tenantId);
                    if (hasAdmin == null) {
                        hasAdmin = "1";
                    }
                    sysUser.setHasAdmin(Integer.valueOf(hasAdmin));
                    if (StrUtil.isNotEmpty(roles)) {
                        List<SysRole> sysRoleList =
                                Arrays.stream(roles.split(","))
                                        .map(role -> {
                                            SysRole sysRole = new SysRole();
                                            sysRole.setRoleCode(role);
                                            return sysRole;
                                        })
                                        .collect(Collectors.toList());
                        sysUser.setRoles(sysRoleList);
                    }
                    userInfoCacheMap.put(cacheKey, sysUser);
                }
            }
            return sysUser;
        }
        return null;
    }

    public static String buildCacheKey(String username, String userId, String tenantId, String roles) {
        String buildCacheKey = String.format("%s:%s:%s:%s", username, userId, tenantId, roles);
        return DigestUtils.md5DigestAsHex(buildCacheKey.getBytes(StandardCharsets.UTF_8));
    }
}
